package com.test.shiro.realm;

import com.test.shiro.domain.entity.UserInfo;
import com.test.shiro.mapper.UserInfoMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

/**
 * @ClassName MyRealm
 * @Author lulei
 * @Data 2020/3/1 15:54
 */
public class MyRealm extends AuthorizingRealm {

    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public String getName() {
        return "MyRealm";
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //第一步：获取用户名
        String userName= (String) authenticationToken.getPrincipal();
        //通过用户名查询用户对象

        UserInfo user = userInfoMapper.findPsd(userName);

        if( user == null){
            return null;
        }
        //假设查询出来了

        UserInfo user_active = userInfoMapper.findPsd(userName);


        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                user,
                user.getPassword(),
                getName());

        return simpleAuthenticationInfo;
    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
}
